/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */

package com.ecarx.systemui.plugin.ui.dialog;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.appcompat.content.res.AppCompatResources;

import com.ecarx.systemui.plugin.AppConstants;
import com.ecarx.systemui.plugin.R;
import com.ecarx.systemui.plugin.ui.dialog.base.AutoCloseDialog;
import com.ecarx.systemui.plugin.ui.dialog.base.DialogManager;
import com.ecarx.systemui.plugin.model.listener.NoDoubleClickListener;
import com.ecarx.systemui.plugin.model.listener.SeatDialogCallBack;
import com.ecarx.systemui.plugin.utils.DensityUtils;
import com.ecarx.systemui.plugin.utils.LogUtils;


public class DriverSeatWheelDialog extends AutoCloseDialog {

    private static final String TAG = "DriverSeatWheelDialog";
    public static final int DIALOG_TOTAL_WIDTH = DensityUtils.dp2px(374);
    public static final int ICON_WITH = DensityUtils.dp2px(94);

    private LinearLayout llRoot;
    private ImageView ivWheel;
    private ImageView ivSeatHeat;
    private ImageView ivSeatVent;
    private int[] location;

    public DriverSeatWheelDialog(int[] location, @NonNull Context context) {
        this(context, 0);
        this.location = location;
    }

    public DriverSeatWheelDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    private SeatDialogCallBack seatDialogCallBack;

    public void setDialogCallBack(SeatDialogCallBack dialogCallBack) {
        this.seatDialogCallBack = dialogCallBack;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_driver_wheel_seat);
        initView();
        initConfig();
    }

    private void initView() {
        llRoot = findViewById(R.id.ll_root);
        ivWheel = findViewById(R.id.iv_wheel);
        ivSeatHeat = findViewById(R.id.iv_seat_heat);
        ivSeatVent = findViewById(R.id.iv_seat_vent);

        ivWheel.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                LogUtils.d(TAG, "Dialog IvWheel Click");
                startTimingClose();
                if (seatDialogCallBack != null) {
                    seatDialogCallBack.adjustWheelHeat();
                }
            }
        });

        ivSeatHeat.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                LogUtils.d(TAG, "Dialog IvSeatHeat Click");
                startTimingClose();
                if (seatDialogCallBack != null) {
                    seatDialogCallBack.adjustDriverSeatHeat();
                }
            }
        });

        ivSeatVent.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                LogUtils.d(TAG, "Dialog IvSeatVent Click");
                startTimingClose();
                if (seatDialogCallBack != null) {
                    seatDialogCallBack.adjustDriverSeatVent();
                }
            }
        });
    }

    @SuppressLint("ClickableViewAccessibility")
    private void initConfig() {
        Window window = getWindow();
        if (window != null) {
            WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.MATCH_PARENT,
                    AppConstants.PLUGIN_DIALOG_TYPE,
                    android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
                            android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN |
                            android.view.WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
                            android.view.WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS |
                            android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                    PixelFormat.TRANSPARENT);
            params.gravity = Gravity.START | Gravity.TOP;
            params.y =  DensityUtils.dp2px(94);
            window.setAttributes(params);
            WindowManager.LayoutParams attributes = window.getAttributes();
            attributes.height = DensityUtils.getScreenHeight() - DensityUtils.dp2px(94);
            window.setAttributes(attributes);
            window.getDecorView().setOnTouchListener((v, event) -> {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    delayedDismiss();
                }
                return false;
            });
        }
        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) llRoot.getLayoutParams();
        layoutParams.leftMargin = location[0];
        layoutParams.gravity = Gravity.START | Gravity.BOTTOM;
        llRoot.setLayoutParams(layoutParams);
    }

    @Override
    public void show() {
        super.show();
        llRoot.startAnimation(enterAnimation);
    }

    @Override
    protected void delayedDismiss() {
        dismiss();
    }

    @Override
    public void dismiss() {
        llRoot.startAnimation(outAnimation);
        llRoot.postDelayed(() -> {
            DriverSeatWheelDialog.super.dismiss();
            DialogManager.getDialogManager().cleanDriverSeatWheelDialog();
        }, 150);
    }

    public void updateConfig(int[] location) {
        this.location = location;
        initConfig();
    }

    public void updateShowIcon(boolean isShowWheel, boolean isShowSeatHeat, boolean isShowSeatVent) {

        LogUtils.d(TAG, "updateShowIcon isShowWheel:" + isShowWheel + ", isShowSeatHeat:"
                + isShowSeatHeat + ", isShowSeatVent:" + isShowSeatVent);
        ivWheel.setVisibility(isShowWheel ? View.VISIBLE : View.GONE);
        ivSeatHeat.setVisibility(isShowSeatHeat ? View.VISIBLE : View.GONE);
        ivSeatVent.setVisibility(isShowSeatVent ? View.VISIBLE : View.GONE);
    }


    public void updateAgainShowIcon(boolean isShowWheel, boolean isShowSeatHeat, boolean isShowSeatVent) {
        startTimingClose();
        updateShowIcon(isShowWheel, isShowSeatHeat, isShowSeatVent);
    }

    public void updateSignalValue(Integer wheelValue, Integer seatHeatValue, Integer seatVentValue) {
        LogUtils.d(TAG, "Driver wheelValue:" + wheelValue + " seatHeatValue:" + seatHeatValue
                + ", seatVentValue:" + seatVentValue);
        if (wheelValue != null && wheelValue != -1) {
            int wheelHeatByWheelHeatValue = getWheelHeatByWheelHeatValue(wheelValue);
            updateImageIcon(ivWheel, wheelHeatByWheelHeatValue);
        }
        if (seatHeatValue != null && seatHeatValue != -1) {
            int heatIconByHeatValue = getHeatIconByHeatValue(seatHeatValue);
            updateImageIcon(ivSeatHeat, heatIconByHeatValue);
        }
        if (seatVentValue != null && seatVentValue != -1) {
            int ventIconByVentValue = getVentIconByVentValue(seatVentValue);
            updateImageIcon(ivSeatVent, ventIconByVentValue);
        }
    }

    private void updateImageIcon(ImageView imageView, @DrawableRes int redId) {
        if (redId != 0) {
            imageView.setImageDrawable(AppCompatResources.getDrawable(getContext(), redId));
        }
    }

    private int getWheelHeatByWheelHeatValue(Integer value) {
        switch (value) {
            case AppConstants.HEAT_LEVEL_0:
                return R.drawable.wheel_heat_0;
            case AppConstants.HEAT_LEVEL_1:
                return R.drawable.wheel_heat_1;
            case AppConstants.HEAT_LEVEL_2:
                return R.drawable.wheel_heat_2;
            case AppConstants.HEAT_LEVEL_3:
                return R.drawable.wheel_heat_3;
        }
        LogUtils.d(TAG, "NO WHEEL HEAT VALUE value:" + value);
        return 0;
    }

    private int getHeatIconByHeatValue(Integer value) {
        switch (value) {
            case AppConstants.HEAT_LEVEL_0:
                return R.drawable.left_seat_heating_off;
            case AppConstants.HEAT_LEVEL_1:
                return R.drawable.left_seat_heating_1;
            case AppConstants.HEAT_LEVEL_2:
                return R.drawable.left_seat_heating_2;
            case AppConstants.HEAT_LEVEL_3:
                return R.drawable.left_seat_heating_3;
        }
        LogUtils.d(TAG, "NO SEAT HEAT VALUE value:" + value);
        return 0;
    }

    private int getVentIconByVentValue(Integer value) {
        switch (value) {
            case AppConstants.HEAT_LEVEL_0:
                return R.drawable.left_seat_ventilation_off;
            case AppConstants.HEAT_LEVEL_1:
                return R.drawable.left_seat_ventilation_1;
            case AppConstants.HEAT_LEVEL_2:
                return R.drawable.left_seat_ventilation_2;
            case AppConstants.HEAT_LEVEL_3:
                return R.drawable.left_seat_ventilation_3;
        }
        LogUtils.d(TAG, "NO SEAT Vent VALUE value:" + value);
        return 0;
    }


}
